package modele;

//Mandatory for chooseNamesByInput() which is currently "commented" because not used.
import java.util.ArrayList;
import java.util.Scanner;

public class Board {

	protected Cell[] lst;
	protected int longueur;
	protected int endCellIndex;

	public Board() {

		longueur = 64;
		lst = new AbstractCell[longueur];
		endCellIndex = longueur - 1;
		initialization();
	}

	/**
	 * Initialize the board with traditional cells positions.
	 */
	public void initialization() {
		for (int i : new int[] { 9, 18, 27, 36, 45, 54 })
			lst[i] = new GooseCell(i);

		lst[6] = new TeleportCell(6, 12);
		lst[42] = new TeleportCell(42, 30);
		lst[58] = new TeleportCell(58, 1);

		for (int i : new int[] { 31, 52 })
			lst[i] = new TrapCell(i);

		lst[19] = new WaitCell(19);

		for (int i = 0; i < longueur; i++) {
			if (lst[i] == null)
				lst[i] = new NormalCell(i);
			// System.out.println("case "+lst[i].getIndex()+" est une case "+lst[i].getTypeCell());
		}
	}

	/**
	 * Method who execute a turn for the selected Player
	 * <code>nextP<code> with a given <code>dice</code> sum
	 * 
	 * @param nextP
	 *            is the player of this turn
	 * @param dice
	 *            is the sum of dice
	 */
	public void playTurn(Player nextP, int dice) {

		// The turn finishes if the player is blocked
		if (!nextP.current_cell.canBeLeftNow()) {
			System.out.println(nextP + " is blocked in cell ["
					+ nextP.current_cell.getIndex() + "] ("
					+ nextP.current_cell.getTypeCell()
					+ ")\n===========================================");
			return;
		}

		System.out.println(nextP + " [" + nextP.getCurentCell().getIndex()
				+ "] throws dice and get " + dice);

		// Compute the destination with current cell and the dice result
		Cell destination = computeDestinationCell(nextP.getCurentCell(), dice);

		// Until the cell sends the player to another cell than the cell itself,
		// change destination
		while (destination.getIndex() != destination.handleMove(dice)) {
			destination.welcome(dice);
			destination = normalizeDestinationCell(destination.handleMove(dice));
		}

		// If a player was in the destination cell, then he goes to current
		// player's current cell
		if (destination.isBusy() && destination.getPlayer() != nextP) {
			System.out.println(destination.getPlayer() + " ---> cell ["
					+ nextP.current_cell.getIndex() + "] ("
					+ nextP.current_cell.getTypeCell() + ")");
			destination.getPlayer().moveTo(nextP.current_cell);
		}

		// Move the player to the destination
		System.out.println(nextP + " ---> cell [" + destination.getIndex()
				+ "] (" + destination.getTypeCell()
				+ ")\n===========================================");
		nextP.moveTo(destination);
	}

	/**
	 * Compute the destination <code>Cell</code> with the old one and a given
	 * dice result
	 * 
	 * @param cell
	 *            is the old cell
	 * @param dice
	 *            is the sum of dice
	 * @return the new <code>Cell</code> of the player
	 */
	public Cell computeDestinationCell(Cell cell, int dice) {
		int resultat = cell.getIndex() + dice;
		if (resultat > endCellIndex) {
			return lst[endCellIndex * 2 - resultat];
		} else
			return lst[resultat];
	}

	/**
	 * Compute the destination <code>Cell</code> with an index which can be
	 * higher than the board's lenght. In this case, this method returns the
	 * index after moving backward on the board. Example : If the index is 65
	 * and the board size is 63. This method returns 61.
	 * 
	 * @return the new <code>Cell</code> of the player
	 */
	public Cell normalizeDestinationCell(int index) {
		if (index > endCellIndex) {
			return lst[endCellIndex * 2 - index];
		} else
			return lst[index];
	}

	/**
	 * This method indicates the last cell of the board depending on its size
	 * 
	 * @return the last Cell
	 */
	public Cell getEndCell() {
		return lst[endCellIndex];
	}

	/**
	 * Ask the user to choose all players names. 
	 * 
	 * @return the players list updated with the names
	 */
	public ArrayList<Player> chooseNamesByInput(ArrayList<Player> players,
			Cell startCell) {
		Scanner a = new Scanner(System.in);
		String input = "";
		int ind = 5;
		

		System.out
				.println("Welcome! Please write players name. Write \'go\' to finish.");
		while (ind > 0) {
			System.out.print("Name? ");
			input = a.nextLine();
			if (input.equals("go"))
				break;
			if (!input.equals(""))
				players.add(new Player(input, startCell));
		}

		return players;
	}
}
